#include "stdafx.h"
#include "MainWnd.h"
#include "AppClient.h"

static std::map<DWORD, CMainWnd*> gs_mapWnd;
const static TCHAR* RegClassName = TEXT("GameGacWnd");
CMainWnd::CMainWnd(CAppClient* pHandler, HINSTANCE hInstance, int nWidth, int nHeight, const TCHAR* szTitle, const TCHAR* szBigIconFileName, const TCHAR* szSmallIconFileName)
{
	m_pHandler = pHandler;
	DWORD dwStyle = WS_OVERLAPPEDWINDOW;

	RECT ClientRect = { 0, 0, nWidth, nHeight };
	m_fWndRatio = float(nHeight) / float(nWidth);
	AdjustWindowRect(&ClientRect, dwStyle, false);
	RECT workArea;
	SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
	nWidth = min(ClientRect.right - ClientRect.left, workArea.right - workArea.left);
	nHeight = min(ClientRect.bottom - ClientRect.top, workArea.bottom - workArea.top);
	int nPosX = workArea.left + (workArea.right - workArea.left - nWidth) / 2;
	int nPosY = workArea.top + (workArea.bottom - workArea.top - nHeight) / 2;
	ATOM rs = RegisterClass(hInstance, 0, 0);

	m_hMainWnd = CreateWindow(RegClassName, TEXT("MinOne"), dwStyle,
		nPosX, nPosY, nWidth, nHeight, NULL, NULL, hInstance, this);

	ShowWindow(m_hMainWnd, TRUE);
	SetWindowPos(m_hMainWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
	UpdateWindow(m_hMainWnd);
}

CMainWnd::CMainWnd(CAppClient* pHandler, HWND hWnd)
{
	m_pHandler = pHandler;
	m_hMainWnd = hWnd;
}

CMainWnd::~CMainWnd()
{
	//CAppClient::Destroy();
	m_pHandler = NULL;
}

LRESULT CALLBACK CMainWnd::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	std::map<DWORD, CMainWnd*>::iterator it = gs_mapWnd.find(reinterpret_cast<DWORD>(hWnd));
	CMainWnd* pGacMainWnd;
	if (it == gs_mapWnd.end())
	{
		if (message != WM_CREATE)
			return ::DefWindowProc(hWnd, message, wParam, lParam);
		pGacMainWnd = reinterpret_cast<CMainWnd*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
		if (pGacMainWnd)
		{
			pGacMainWnd->m_hMainWnd = hWnd;
			gs_mapWnd[reinterpret_cast<DWORD>(hWnd)] = pGacMainWnd;
		}
	}
	else
	{
		pGacMainWnd = it->second;
	}
	return pGacMainWnd->OnWndMessage(message, wParam, lParam);
}

ATOM CMainWnd::RegisterClass(HINSTANCE hInstance, HICON hBigIcon, HICON hSmallIcon)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = CMainWnd::WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = NULL;// LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = RegClassName;
	wcex.hIconSm = NULL;
	return RegisterClassEx(&wcex);
}

LRESULT CMainWnd::OnWndMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (((WM_MOUSEFIRST <= uMsg && uMsg <= WM_MOUSELAST) || (WM_KEYFIRST <= uMsg && uMsg <= WM_KEYLAST)) && m_pHandler)
	{
		return m_pHandler->OnInputMessage(uMsg, wParam, lParam);
	}
	else
	{
		
		if( uMsg == WM_CLOSE )
		{
			m_pHandler->Quit();
			return 0;
		}
	}
	return DefWindowProc(m_hMainWnd, uMsg, wParam, lParam);
}

void CMainWnd::Show(bool bShow)
{
	::ShowWindow(m_hMainWnd, bShow ? SW_SHOW : SW_HIDE);
}

bool CMainWnd::IsActive() const
{
	return m_hMainWnd == GetForegroundWindow();
}

HWND CMainWnd::GetHwnd() const
{
	return m_hMainWnd;
}